<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>pg_stat_statements</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.1.2 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Additional Supplied Modules"
HREF="contrib.html"><LINK
REL="PREVIOUS"
TITLE="pg_standby"
HREF="pgstandby.html"><LINK
REL="NEXT"
TITLE="pgstattuple"
HREF="pgstattuple.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2011-12-01T22:07:59"></HEAD
><BODY
CLASS="SECT1"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.1.2 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="pg_standby"
HREF="pgstandby.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="contrib.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
>Appendix F. Additional Supplied Modules</TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="pgstattuple"
HREF="pgstattuple.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="PGSTATSTATEMENTS"
>F.32. pg_stat_statements</A
></H1
><P
>  The <TT
CLASS="FILENAME"
>pg_stat_statements</TT
> module provides a means for
  tracking execution statistics of all SQL statements executed by a server.
 </P
><P
>  The module must be loaded by adding <TT
CLASS="LITERAL"
>pg_stat_statements</TT
> to
  <A
HREF="runtime-config-resource.html#GUC-SHARED-PRELOAD-LIBRARIES"
>shared_preload_libraries</A
> in
  <TT
CLASS="FILENAME"
>postgresql.conf</TT
>, because it requires additional shared memory.
  This means that a server restart is needed to add or remove the module.
 </P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN135427"
>F.32.1. The <TT
CLASS="STRUCTNAME"
>pg_stat_statements</TT
> View</A
></H2
><P
>   The statistics gathered by the module are made available via a system view
   named <TT
CLASS="STRUCTNAME"
>pg_stat_statements</TT
>.  This view contains one row for
   each distinct query text, database ID, and user ID (up to the maximum
   number of distinct statements that the module can track).  The columns
   of the view are shown in <A
HREF="pgstatstatements.html#PGSTATSTATEMENTS-COLUMNS"
>Table F-20</A
>.
  </P
><DIV
CLASS="TABLE"
><A
NAME="PGSTATSTATEMENTS-COLUMNS"
></A
><P
><B
>Table F-20. <TT
CLASS="STRUCTNAME"
>pg_stat_statements</TT
> Columns</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><COL><COL><THEAD
><TR
><TH
>Name</TH
><TH
>Type</TH
><TH
>References</TH
><TH
>Description</TH
></TR
></THEAD
><TBODY
><TR
><TD
><TT
CLASS="STRUCTFIELD"
>userid</TT
></TD
><TD
><TT
CLASS="TYPE"
>oid</TT
></TD
><TD
><TT
CLASS="LITERAL"
><A
HREF="catalog-pg-authid.html"
><TT
CLASS="STRUCTNAME"
>pg_authid</TT
></A
>.oid</TT
></TD
><TD
>OID of user who executed the statement</TD
></TR
><TR
><TD
><TT
CLASS="STRUCTFIELD"
>dbid</TT
></TD
><TD
><TT
CLASS="TYPE"
>oid</TT
></TD
><TD
><TT
CLASS="LITERAL"
><A
HREF="catalog-pg-database.html"
><TT
CLASS="STRUCTNAME"
>pg_database</TT
></A
>.oid</TT
></TD
><TD
>OID of database in which the statement was executed</TD
></TR
><TR
><TD
><TT
CLASS="STRUCTFIELD"
>query</TT
></TD
><TD
><TT
CLASS="TYPE"
>text</TT
></TD
><TD
>&nbsp;</TD
><TD
>Text of the statement (up to <A
HREF="runtime-config-statistics.html#GUC-TRACK-ACTIVITY-QUERY-SIZE"
>track_activity_query_size</A
> bytes)</TD
></TR
><TR
><TD
><TT
CLASS="STRUCTFIELD"
>calls</TT
></TD
><TD
><TT
CLASS="TYPE"
>bigint</TT
></TD
><TD
>&nbsp;</TD
><TD
>Number of times executed</TD
></TR
><TR
><TD
><TT
CLASS="STRUCTFIELD"
>total_time</TT
></TD
><TD
><TT
CLASS="TYPE"
>double precision</TT
></TD
><TD
>&nbsp;</TD
><TD
>Total time spent in the statement, in seconds</TD
></TR
><TR
><TD
><TT
CLASS="STRUCTFIELD"
>rows</TT
></TD
><TD
><TT
CLASS="TYPE"
>bigint</TT
></TD
><TD
>&nbsp;</TD
><TD
>Total number of rows retrieved or affected by the statement</TD
></TR
><TR
><TD
><TT
CLASS="STRUCTFIELD"
>shared_blks_hit</TT
></TD
><TD
><TT
CLASS="TYPE"
>bigint</TT
></TD
><TD
>&nbsp;</TD
><TD
>Total number of shared blocks hits by the statement</TD
></TR
><TR
><TD
><TT
CLASS="STRUCTFIELD"
>shared_blks_read</TT
></TD
><TD
><TT
CLASS="TYPE"
>bigint</TT
></TD
><TD
>&nbsp;</TD
><TD
>Total number of shared blocks reads by the statement</TD
></TR
><TR
><TD
><TT
CLASS="STRUCTFIELD"
>shared_blks_written</TT
></TD
><TD
><TT
CLASS="TYPE"
>bigint</TT
></TD
><TD
>&nbsp;</TD
><TD
>Total number of shared blocks writes by the statement</TD
></TR
><TR
><TD
><TT
CLASS="STRUCTFIELD"
>local_blks_hit</TT
></TD
><TD
><TT
CLASS="TYPE"
>bigint</TT
></TD
><TD
>&nbsp;</TD
><TD
>Total number of local blocks hits by the statement</TD
></TR
><TR
><TD
><TT
CLASS="STRUCTFIELD"
>local_blks_read</TT
></TD
><TD
><TT
CLASS="TYPE"
>bigint</TT
></TD
><TD
>&nbsp;</TD
><TD
>Total number of local blocks reads by the statement</TD
></TR
><TR
><TD
><TT
CLASS="STRUCTFIELD"
>local_blks_written</TT
></TD
><TD
><TT
CLASS="TYPE"
>bigint</TT
></TD
><TD
>&nbsp;</TD
><TD
>Total number of local blocks writes by the statement</TD
></TR
><TR
><TD
><TT
CLASS="STRUCTFIELD"
>temp_blks_read</TT
></TD
><TD
><TT
CLASS="TYPE"
>bigint</TT
></TD
><TD
>&nbsp;</TD
><TD
>Total number of temp blocks reads by the statement</TD
></TR
><TR
><TD
><TT
CLASS="STRUCTFIELD"
>temp_blks_written</TT
></TD
><TD
><TT
CLASS="TYPE"
>bigint</TT
></TD
><TD
>&nbsp;</TD
><TD
>Total number of temp blocks writes by the statement</TD
></TR
></TBODY
></TABLE
></DIV
><P
>   This view, and the function <CODE
CLASS="FUNCTION"
>pg_stat_statements_reset</CODE
>,
   are available only in databases they have been specifically installed into
   by installing the <TT
CLASS="LITERAL"
>pg_stat_statements</TT
> extension.
   However, statistics are tracked across all databases of the server
   whenever the <TT
CLASS="FILENAME"
>pg_stat_statements</TT
> module is loaded
   into the server, regardless of presence of the view.
  </P
><P
>   For security reasons, non-superusers are not allowed to see the text of
   queries executed by other users.  They can see the statistics, however,
   if the view has been installed in their database.
  </P
><P
>   Note that statements are considered the same if they have the same text,
   regardless of the values of any out-of-line parameters used in the
   statement.  Using out-of-line parameters will help to group statements
   together and may make the statistics more useful.
  </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN135555"
>F.32.2. Functions</A
></H2
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><CODE
CLASS="FUNCTION"
>pg_stat_statements_reset() returns void</CODE
></DT
><DD
><P
>      <CODE
CLASS="FUNCTION"
>pg_stat_statements_reset</CODE
> discards all statistics
      gathered so far by <TT
CLASS="FILENAME"
>pg_stat_statements</TT
>.
      By default, this function can only be executed by superusers.
     </P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN135565"
>F.32.3. Configuration Parameters</A
></H2
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="VARNAME"
>pg_stat_statements.max</TT
> (<TT
CLASS="TYPE"
>integer</TT
>)</DT
><DD
><P
>      <TT
CLASS="VARNAME"
>pg_stat_statements.max</TT
> is the maximum number of
      statements tracked by the module (i.e., the maximum number of rows
      in the <TT
CLASS="STRUCTNAME"
>pg_stat_statements</TT
> view).  If more distinct
      statements than that are observed, information about the least-executed
      statements is discarded.
      The default value is 1000.
      This parameter can only be set at server start.
     </P
></DD
><DT
><TT
CLASS="VARNAME"
>pg_stat_statements.track</TT
> (<TT
CLASS="TYPE"
>enum</TT
>)</DT
><DD
><P
>      <TT
CLASS="VARNAME"
>pg_stat_statements.track</TT
> controls which statements
      are counted by the module.
      Specify <TT
CLASS="LITERAL"
>top</TT
> to track top-level statements (those issued
      directly by clients), <TT
CLASS="LITERAL"
>all</TT
> to also track nested statements
      (such as statements invoked within functions), or <TT
CLASS="LITERAL"
>none</TT
> to
      disable.
      The default value is <TT
CLASS="LITERAL"
>top</TT
>.
      Only superusers can change this setting.
     </P
></DD
><DT
><TT
CLASS="VARNAME"
>pg_stat_statements.track_utility</TT
> (<TT
CLASS="TYPE"
>boolean</TT
>)</DT
><DD
><P
>      <TT
CLASS="VARNAME"
>pg_stat_statements.track_utility</TT
> controls whether
      utility commands are tracked by the module.  Utility commands are
      all those other than <TT
CLASS="COMMAND"
>SELECT</TT
>, <TT
CLASS="COMMAND"
>INSERT</TT
>,
      <TT
CLASS="COMMAND"
>UPDATE</TT
> and <TT
CLASS="COMMAND"
>DELETE</TT
>.
      The default value is <TT
CLASS="LITERAL"
>on</TT
>.
      Only superusers can change this setting.
     </P
></DD
><DT
><TT
CLASS="VARNAME"
>pg_stat_statements.save</TT
> (<TT
CLASS="TYPE"
>boolean</TT
>)</DT
><DD
><P
>      <TT
CLASS="VARNAME"
>pg_stat_statements.save</TT
> specifies whether to
      save statement statistics across server shutdowns.
      If it is <TT
CLASS="LITERAL"
>off</TT
> then statistics are not saved at
      shutdown nor reloaded at server start.
      The default value is <TT
CLASS="LITERAL"
>on</TT
>.
      This parameter can only be set in the <TT
CLASS="FILENAME"
>postgresql.conf</TT
>
      file or on the server command line.
     </P
></DD
></DL
></DIV
><P
>   The module requires additional shared memory amounting to about
   <TT
CLASS="VARNAME"
>pg_stat_statements.max</TT
> <TT
CLASS="LITERAL"
>*</TT
>
   <A
HREF="runtime-config-statistics.html#GUC-TRACK-ACTIVITY-QUERY-SIZE"
>track_activity_query_size</A
> bytes.  Note that this
   memory is consumed whenever the module is loaded, even if
   <TT
CLASS="VARNAME"
>pg_stat_statements.track</TT
> is set to <TT
CLASS="LITERAL"
>none</TT
>.
  </P
><P
>   In order to set any of these parameters in your
   <TT
CLASS="FILENAME"
>postgresql.conf</TT
> file,
   you will need to add <TT
CLASS="LITERAL"
>pg_stat_statements</TT
> to
   <A
HREF="runtime-config-custom.html#GUC-CUSTOM-VARIABLE-CLASSES"
>custom_variable_classes</A
>.  Typical usage might be:

</P><PRE
CLASS="PROGRAMLISTING"
># postgresql.conf
shared_preload_libraries = 'pg_stat_statements'

custom_variable_classes = 'pg_stat_statements'
pg_stat_statements.max = 10000
pg_stat_statements.track = all</PRE
><P>
  </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN135620"
>F.32.4. Sample Output</A
></H2
><PRE
CLASS="SCREEN"
>bench=# SELECT pg_stat_statements_reset();

$ pgbench -i bench
$ pgbench -c10 -t300 -M prepared bench

bench=# \x
bench=# SELECT query, calls, total_time, rows, 100.0 * shared_blks_hit /
               nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent
          FROM pg_stat_statements ORDER BY total_time DESC LIMIT 5;
-[ RECORD 1 ]---------------------------------------------------------------------
query       | UPDATE pgbench_branches SET bbalance = bbalance + $1 WHERE bid = $2;
calls       | 3000
total_time  | 9.60900100000002
rows        | 2836
hit_percent | 99.9778970000200936
-[ RECORD 2 ]---------------------------------------------------------------------
query       | UPDATE pgbench_tellers SET tbalance = tbalance + $1 WHERE tid = $2;
calls       | 3000
total_time  | 8.015156
rows        | 2990
hit_percent | 99.9731126579631345
-[ RECORD 3 ]---------------------------------------------------------------------
query       | copy pgbench_accounts from stdin
calls       | 1
total_time  | 0.310624
rows        | 100000
hit_percent | 0.30395136778115501520
-[ RECORD 4 ]---------------------------------------------------------------------
query       | UPDATE pgbench_accounts SET abalance = abalance + $1 WHERE aid = $2;
calls       | 3000
total_time  | 0.271741999999997
rows        | 3000
hit_percent | 93.7968855088209426
-[ RECORD 5 ]---------------------------------------------------------------------
query       | alter table pgbench_accounts add primary key (aid)
calls       | 1
total_time  | 0.08142
rows        | 0
hit_percent | 34.4947735191637631</PRE
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN135623"
>F.32.5. Author</A
></H2
><P
>   Takahiro Itagaki <CODE
CLASS="EMAIL"
>&#60;<A
HREF="mailto:itagaki.takahiro@oss.ntt.co.jp"
>itagaki.takahiro@oss.ntt.co.jp</A
>&#62;</CODE
>
  </P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="pgstandby.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="pgstattuple.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>pg_standby</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="contrib.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>pgstattuple</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>